package com.aier.school.common;

import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.aier.common.common.Constant;
import com.aier.school.entity.TeacherAttendanceMonth;
import com.aier.school.entity.TeacherIcardInfo;
import com.aier.school.entity.TeacherLeave;
import com.aier.school.entity.UserIcardInfo;
import com.aier.school.entity.UserIcardRecord;

public class SchoolAttendanceUtil {
    public static final String SHEETNAME_ATTENDANCE = "每月考勤";
    public static final String SHEETNAME_INFO = "考勤明细";
    public static final String LEAVE_INFO = "异常信息";
    public static final String[] ATTENDANCE_MONTH_TITLE = 
            new String[]{"姓名","职务","全勤","实际考勤","缺勤","迟到","早退","累计缺勤(分钟)","请假(小时)","加班(小时)","年月","异常时长"};
    public static final String[] ICARD_INFO_TITLE = new String[]{"员工姓名","出入校园","时间"};
    public static final String[] LEAVE_INFO_TITLE = new String[]{"姓名","开始时间","结束时间","请假时长(小时)","类型","事由","状态"};
    
    public static final String SHEETNAME_USERICARD = "学生打卡记录";
    public static final String[] STU_ICARD_INFO_TITLE = new String[]{"学生姓名","出入校园","时间"};
    
    public static final String SHEETNAME_TEAICARD = "教师打卡记录";
    public static Integer TYPE;
    public static final String[] TEA_ICARD_INFO_TITLE_DAY = new String[]{"教师姓名","上午进园","上午出园","下午进园","下午出园"};
    public static final String[] TEA_ICARD_INFO_TITLE_PERSON = new String[]{"时间","上午进园","上午出园","下午进园","下午出园"};
    
    /**
     * 通过map生成工作簿
     * @param map 要写入的数据
     */
    public static HSSFWorkbook  createExcel(List<TeacherAttendanceMonth> attMonthList,
            List<TeacherIcardInfo> attDetailList,List<TeacherLeave> terLeaveList){
        //String sheetName;
        //生成一个工作簿
        HSSFWorkbook wb = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet;

        sheet = wb.createSheet(SHEETNAME_ATTENDANCE);
        writeToSheet(SHEETNAME_ATTENDANCE, sheet,attMonthList );    
    
        sheet = wb.createSheet(SHEETNAME_INFO);
        writeToSheet(SHEETNAME_INFO, sheet, attDetailList);
        
        sheet = wb.createSheet(LEAVE_INFO);
        writeToSheet(LEAVE_INFO, sheet, terLeaveList);
    
        return wb;
        
    }
    
    public static HSSFWorkbook createUIIExcel(List<UserIcardInfo> icardInfoList){
        //String sheetName;
        //生成一个工作簿
        HSSFWorkbook wb = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet;

        sheet = wb.createSheet(SHEETNAME_USERICARD);
        writeToSheet(SHEETNAME_USERICARD, sheet,icardInfoList );    
    
        return wb;
        
    }
    
    public static HSSFWorkbook createTIIExcel(List<UserIcardRecord> icardInfoList,Integer type){
        //String sheetName;
        //生成一个工作簿
        HSSFWorkbook wb = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet;

        sheet = wb.createSheet(SHEETNAME_USERICARD);
        TYPE = type;
        writeToSheet(SHEETNAME_TEAICARD, sheet,icardInfoList );    
        
        return wb;
        
    }
    
    
  /**
    * 把数据    写入当前表中
    * @param sheetName    表名称
    * @param sheet        要操作的表
    * @param content    要写入的数据
    */
    @SuppressWarnings("unchecked")
    private static void writeToSheet(String sheetName,HSSFSheet sheet,List list){
        HSSFRow row;
//        HSSFCell cell;
        List<TeacherAttendanceMonth> attMonthList;
        List<TeacherIcardInfo> attDetailList;
        List<TeacherLeave> terLeaveList;
        List<UserIcardInfo> icardInfoList;
        List<UserIcardRecord> icardRecordList;
        // 设置表格默认列宽度为10个字节
        sheet.setDefaultColumnWidth(Constant.num_10);
        //月考勤
        if(SHEETNAME_ATTENDANCE.equals(sheetName)){
            attMonthList = list;
            writeTitleInSheet(sheetName, sheet, ATTENDANCE_MONTH_TITLE);
            if(attMonthList!=null){
                for(int i = 0; i<attMonthList.size(); i++){
                    row = sheet.createRow(i+1);
                    TeacherAttendanceMonth attMonth = attMonthList.get(i);  
                    writeToCell(row.createCell(0), attMonth.getTeacherName().toString());
                    writeToCell(row.createCell(1), attMonth.getTypeName().toString());
                    writeToCell(row.createCell(2), attMonth.getFull().toString());
                    writeToCell(row.createCell(Constant.num_3), attMonth.getAttendance().toString());
                    writeToCell(row.createCell(Constant.num_4), attMonth.getAbsenteeism().toString());
                    writeToCell(row.createCell(Constant.num_5), attMonth.getLate().toString());
                    writeToCell(row.createCell(Constant.num_6), attMonth.getEarly().toString());
                    writeToCell(row.createCell(Constant.num_7), attMonth.getAccumulation().toString());
                    writeToCell(row.createCell(Constant.num_8), attMonth.getLeave().toString());
                    writeToCell(row.createCell(Constant.num_9), attMonth.getOvertime().toString());
                    writeToCell(row.createCell(Constant.num_10), attMonth.getDate().toString());
                    writeToCell(row.createCell(Constant.num_11), attMonth.getTime_exc().toString());
                }                 
            }
        
    
        }//考情明细
        else if(SHEETNAME_INFO.equals(sheetName)){    
            attDetailList = list;
            writeTitleInSheet(sheetName, sheet, ICARD_INFO_TITLE);
            if(attDetailList!=null){
            	if(attDetailList.size()>Constant.num_65500){
            	    attDetailList.subList(0, Constant.num_65500);
            	}
                for(int i = 0; i<attDetailList.size(); i++){
                    row = sheet.createRow(i+1);
                    TeacherIcardInfo attDetail = attDetailList.get(i);  
                    writeToCell(row.createCell(0), attDetail.getTeacherName().toString());
                    writeToCell(row.createCell(1), attDetail.getType_str().toString());
                    writeToCell(row.createCell(2), attDetail.getCdate_str().toString());
                }                   
            }

        }//异常信息
        else if(LEAVE_INFO.equals(sheetName)){
            terLeaveList = list;
            writeTitleInSheet(sheetName, sheet, LEAVE_INFO_TITLE);
            if(terLeaveList!=null){
                for(int i = 0; i<terLeaveList.size(); i++){
                    row = sheet.createRow(i+1);
                    TeacherLeave terLeave = terLeaveList.get(i);  
                    writeToCell(row.createCell(0), terLeave.getTeacherName().toString());
                    writeToCell(row.createCell(1), terLeave.getSdate_str().toString());
                    writeToCell(row.createCell(2), terLeave.getEdate_str().toString());
                    writeToCell(row.createCell(Constant.num_3), terLeave.getLeaveTime().toString());
                    writeToCell(row.createCell(Constant.num_4), terLeave.getType_handle_str().toString());
                    writeToCell(row.createCell(Constant.num_5), terLeave.getDesinfo().toString());
                    writeToCell(row.createCell(Constant.num_6), terLeave.getState_str().toString());
                }                 
            }
  
            //设置特定的列宽
//            sheet.setColumnWidth(Constant.num_5, Constant.num_5000);
//            sheet.setColumnWidth(Constant.num_6, Constant.num_3500);
           
        } else if (SHEETNAME_USERICARD.equals(sheetName)) {
            icardInfoList = list;
            // 学生打卡记录
            writeTitleInSheet(sheetName, sheet, STU_ICARD_INFO_TITLE);
            if(icardInfoList!=null){
                if(icardInfoList.size()>Constant.num_65500){
                    icardInfoList.subList(0, Constant.num_65500);
                }
                for(int i = 0; i<icardInfoList.size(); i++){
                    row = sheet.createRow(i+1);
                    UserIcardInfo icardInfo = icardInfoList.get(i);  
                    writeToCell(row.createCell(0), icardInfo.getName_student().toString());
                    writeToCell(row.createCell(1), icardInfo.getType_str().toString());
                    writeToCell(row.createCell(2), icardInfo.getCdate_str().toString());
                }                   
            }
        } else if (SHEETNAME_TEAICARD.equals(sheetName)) {
            icardRecordList = list;
            // 教师打卡记录
            if (TYPE == null || TYPE == 1) {
                writeTitleInSheet(sheetName, sheet, TEA_ICARD_INFO_TITLE_DAY);
            } else {
                writeTitleInSheet(sheetName, sheet, TEA_ICARD_INFO_TITLE_PERSON);
            }
            if(icardRecordList!=null){
                if(icardRecordList.size()>Constant.num_65500){
                    icardRecordList.subList(0, Constant.num_65500);
                }
                for(int i = 0; i<icardRecordList.size(); i++){
                    row = sheet.createRow(i+1);
                    UserIcardRecord icardInfo = icardRecordList.get(i);  
                    writeToCell(row.createCell(0), icardInfo.getName().toString());
                    writeToCell(row.createCell(1), icardInfo.getAm_in().toString());
                    writeToCell(row.createCell(2), icardInfo.getAm_out().toString());
                    writeToCell(row.createCell(Constant.num_3), icardInfo.getPm_in().toString());
                    writeToCell(row.createCell(Constant.num_4), icardInfo.getPm_out().toString());
                }                   
            }
            
        } 
    }    
       
    /**
     * 把标题栏写入表中
     * @param sheetName     表名
     * @param sheet                要操作的表
     * @param title                表的标题栏
     */
    private static void writeTitleInSheet(String sheetName,HSSFSheet sheet,String[] title){
        HSSFRow row;
        HSSFCell cell; 
        //产生表格标题行
        row = sheet.createRow(0);
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);      
            writeToCell(cell, title[i]);                   
        }    
    }
       
    /**
     *把数据写入的当前列中                
     * @param cell     要操作的列
     * @param content    要输入的内容
     */
    private static void writeToCell(HSSFCell cell,String content){
        HSSFRichTextString text;
        text = new HSSFRichTextString(content);
        cell.setCellValue(text);     
    }
    
}
